package com.itheima.reggie.service.impl;

import com.itheima.reggie.entity.Address;
import com.itheima.reggie.exception.BusinessException;
import com.itheima.reggie.mapper.AddressMapper;
import com.itheima.reggie.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class AddressServiceImpl implements AddressService  {

    @Autowired
    private AddressMapper addressMapper ;

    //查询所有的地址
    @Override
    public List<Address> List(Long userId) {
        return addressMapper.list( userId );
    }

    //添加地址
    @Override
    public void addAddress(Address address) {

        address.setCreateTime(LocalDateTime.now());

        address.setUpdateTime(LocalDateTime.now());

        address.setIsDefault(0);

        addressMapper.add(address);
    }

    //修改默认值
    @Override
    public void updateDefault( Long addressId , Long userId) {

        //先将该用户全部的地址都变成不默认
        addressMapper.updateDefault1(userId);
        //然后根据地址id来修改默认值
        addressMapper.updateDefault2(addressId);

    }

    //根据id查询地址数据
    @Override
    public Address findById(Long id) {
        return addressMapper.findById (id);
    }

    //根据id更新数据
    @Override
    public void updateAddress(Address address) {
        address.setUpdateTime(LocalDateTime.now());
        throw new BusinessException("对不起，这个功能还没有开发");
    }

    //查询默认地址
    @Override
    public Address findDefatult(Long userId) {
        return addressMapper.findDefault(userId);
    }

}
